package com.jrx.anytxn.transaction.job.customer.config;

import com.jrx.anytxn.common.constant.Constants;
import com.jrx.anytxn.customer.entity.CmLimitAdjustLog;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.job.customer.step.CardLogOffProcessor;
import com.jrx.anytxn.transaction.job.customer.step.CardLogOffWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @comment: 卡注销
 * @author: lbw
 * @date: 2024/05/08 15:00
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
@EnableBatchProcessing
public class CardLogOffConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    CardLogOffProcessor cardLogOffProcessor;
    @Autowired
    CardLogOffWriter cardLogOffWriter;
    @Autowired
    PlatformTransactionManager platformTransactionManager;

    @Bean
    public Job cardLogOffJob() {
        return jobBuilderFactory.get("cardLogOffJob")
                .incrementer(new RunIdIncrementer())
                .start(queryCardLogOffUser())
                .build();
    }

    @Bean
    public Step queryCardLogOffUser() {
        return stepBuilderFactory.get("queryCardLogOffUser")
                .<CmLimitAdjustLog, CmLimitAdjustLog>chunk(anytxnBatchProperties.getDefaultConfig().getChunkLimit())
                .reader(cardLogOffReader())
                .processor(cardLogOffProcessor)
                .writer(cardLogOffWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getDefaultConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<CmLimitAdjustLog> cardLogOffReader() {
        MyBatisCursorItemReader<CmLimitAdjustLog> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        cursorItemReader.setQueryId("com.jrx.anytxn.customer.mapper.ext.ExtCmLimitAdjustLogMapper.findNeverCardLogOffUser");
        //查询参数设置
        Map<String, Object> map = new HashMap<>();
        map.put("tenantId", Constants.DEFAULT_TENANT_ID);
        cursorItemReader.setParameterValues(map);
        SynchronizedItemStreamReader<CmLimitAdjustLog> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }

}
